﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace RI
{
    public partial class frm_User : Form
    {
        public frm_User()
        {
            InitializeComponent();
            BindUserList();
        }

        /// <summary>
        /// 绑定用户
        /// </summary>
        public void BindUserList()
        {
            using (Business bus = new Business())
            {
                dgvUser.DataSource = bus.GetAllRegisterUser();                
            }
            for (int i = 0; i < dgvUser.Rows.Count; i++)
            {
                if (dgvUser.Rows[i].Cells[0].Value.ToString() == txtNumber.Text)
                {
                    dgvUser.Rows[i].Selected = true;
                    dgvUser.CurrentCell = dgvUser.Rows[i].Cells[0];
                    break;
                }
            }
        }

        private void dgvUser_MouseClick(object sender, MouseEventArgs e)
        {
            int current = dgvUser.SelectedRows.Count;
            if (current != 0)
            {
                txtNumber.Text = dgvUser.SelectedRows[0].Cells[0].Value.ToString();
                txtTitle.Text = dgvUser.SelectedRows[0].Cells[1].Value.ToString();
                txtHome.Text = dgvUser.SelectedRows[0].Cells[2].Value.ToString();
                txtUsername.Text = dgvUser.SelectedRows[0].Cells[3].Value.ToString();
                txtPassword.Text = dgvUser.SelectedRows[0].Cells[4].Value.ToString();
                txtShop.Text = dgvUser.SelectedRows[0].Cells[5].Value.ToString();
                rtxRemark.Text = dgvUser.SelectedRows[0].Cells[6].Value.ToString();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            BindUserList();
        }

        private void btnDelUser_Click(object sender, EventArgs e)
        {
            if(MessageBox.Show("确定删除吗?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information)
                == DialogResult.Yes)
            {
                DelUser();
                this.BindUserList();
            }
        }

        /// <summary>
        /// 删除用户
        /// </summary>
        public void DelUser()
        {
            if (txtNumber.Text != string.Empty)
            {
                using (Business bus = new Business())
                {
                    bus.DelUser(txtNumber.Text.Trim());                    
                }
                MessageBox.Show("删除成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                MessageBox.Show("请选择用户!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void btnModifyUser_Click(object sender, EventArgs e)
        {
            if (txtNumber.Text != string.Empty)
            {
                using (Business bus = new Business())
                {
                    bus.ModifyUser(txtNumber.Text.Trim(),
                        txtShop.Text.Trim(),
                        txtUsername.Text.Trim(),
                        txtPassword.Text.Trim(),
                        rtxRemark.Text.Trim());
                }
                BindUserList();
            }
            else
            {
                MessageBox.Show("请选择用户!", "提示", 
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Information);
            }
        }
    }
}
